package com.noble.library.permission.andPermission.runtime.setting;

import com.noble.library.permission.andPermission.PermissionActivity;
import com.noble.library.permission.andPermission.Setting;
import com.noble.library.permission.andPermission.source.Source;
import com.noble.library.permission.andPermission.util.MainExecutor;

/**
 * <p>Setting executor.</p>
 * Created by Yan Zhenjie on 2016/12/28.
 */
public class RuntimeSetting implements Setting, PermissionActivity.RequestListener {

    private static final MainExecutor EXECUTOR = new MainExecutor();

    private Source mSource;
    private Action mComeback;

    public RuntimeSetting(Source source) {
        this.mSource = source;
    }

    @Override
    public void execute() {
        new RuntimeSettingPage(mSource).start(-1);
    }

    @Override
    public void execute(int requestCode) {
        new RuntimeSettingPage(mSource).start(requestCode);
    }

    @Override
    public void cancel() {
    }

    @Override
    public Setting onComeback(Action comeback) {
        this.mComeback = comeback;
        return this;
    }

    @Override
    public void start() {
        PermissionActivity.permissionSetting(mSource.getContext(), this);
    }

    @Override
    public void onRequestCallback() {
        EXECUTOR.postDelayed(() -> {
            if (mComeback != null) {
                mComeback.onAction();
            }
        }, 100);
    }
}